﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ClientRegister
{
    public partial class ChangeInfoEmpForm : Form
    {
        public string ID { set; get; }
        public string Name { set; get; }
        public string pass { set; get; }
        public string gender { set; get; }
        public string birthday { set; get; }
        public string addr { set; get; }
        public string phone { set; get; }
        public string role { set; get; }
        public string note { set; get; }

        public ChangeInfoEmpForm()
        {
            InitializeComponent();
        }

        private void ChangeInfoEmpForm_Load(object sender, EventArgs e)
        {
            txtName.Text = Name;
            txtpass.Text = pass;
            txtGender.Text = gender;
            txtBirth.Text = DateTime.Parse(birthday).ToShortDateString();
            txtAddr.Text = addr;
            txtPhone.Text = phone;
            txtRole.Text = role;           
        }

        private void linklblGender_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            cbbGender.Visible = true;
            GenderDataProvider gender = new GenderDataProvider();
            cbbGender.DataSource = gender.loadDs().Tables[0];
            cbbGender.DisplayMember = "Gender";
            cbbGender.ValueMember = "IDGender";
        }

        private void linklblRole_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            cbbRole.Visible = true;
            RoleDataProvider role = new RoleDataProvider();
            cbbRole.DataSource = role.loadRole().Tables[0];
            cbbRole.DisplayMember = "Role";
            cbbRole.ValueMember = "IDRole";
        }

        private void cbbGender_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbGender.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                if (txtGender.Text != cbbGender.SelectedValue.ToString())
                {
                    txtGender.Text = cbbGender.SelectedValue.ToString();
                }
            }
        }

        private void cbbRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbRole.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                if (txtRole.Text != cbbRole.SelectedValue.ToString())
                {
                    txtRole.Text = cbbRole.SelectedValue.ToString();
                }
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (txtNote.Text != "")
            {
                string noteC = note;
                noteC += "- Chinh sua ngay " + DateTime.Now.ToString() + " boi nhan vien: "+ "\n";
                noteC += "- Noi dung: " + txtNote.Text + "\n";
                
                if(txtName.Text != Name)
                     noteC += "- " + Name + "\n";
               
                if(txtAddr.Text != addr)
                     noteC += "- " + addr + "\n";

                if (txtBirth.Text != DateTime.Parse(birthday).ToShortDateString())
                    noteC += "- " + DateTime.Parse(birthday).ToShortDateString() + "\n";
                
                if(txtRole.Text != role)
                     noteC += "- " + role + "\n";
                
                if(txtGender.Text != gender)
                     noteC += "- " + gender + "\n";
                
                if(txtPhone.Text != phone)
                    noteC += "- " + phone + "\n";

                EmployeesDataProvider change = new EmployeesDataProvider();
                if (change.changeInfo(ID, txtName.Text, txtGender.Text, txtBirth.Text, txtAddr.Text, txtPhone.Text, txtRole.Text, noteC) != 0)
                    MessageBox.Show("Đã sửa!");

            }
            else { MessageBox.Show("Cần điền thông tin chỉnh sửa vào mục Ghi chú"); }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
    }
}
